package com.siao.example.common.controller
{
	import com.siao.applications.Application;
	import com.siao.applications.ApplicationManager;
	import com.siao.core.AssetsManager;
	import com.siao.core.Configuration;
	import com.siao.example.common.method.MainMethod;
	import com.siao.example.loading.Loading;
	import com.siao.example.loading.method.LoadMethod;
	import com.siao.mvc.Controller;
	import com.siao.mvc.core.INotice;
	
	/**
	 * 应用程序切换控制器
	 * @author SiaoLeon
	 */
	public class ApplicationSwitchController extends Controller
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var currentApplication:String;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function ApplicationSwitchController()
		{
			super();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		override public function onAdd():void
		{
			this.register(MainMethod.ENTER_EXAMPLE, ENTER_EXAMPLE);
		}
		
		override public function onRemove():void
		{
		
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		private function LOAD(config:String):void
		{
			ApplicationManager.instance.startup(new Loading());
			this.register(LoadMethod.LOAD_COMPLETE, LOAD_COMPLETE);
			this.notify(LoadMethod.LOAD_ASSETS, Configuration.instance.getConfig(config).getOptions());
		}
		
		private function LOAD_COMPLETE(notice:INotice):void
		{
			this.remove(LoadMethod.LOAD_COMPLETE, LOAD_COMPLETE);
			
			var app:Application = AssetsManager.instance.getAsset(this.currentApplication);
			ApplicationManager.instance.startup(app);
		}
		
		private function ENTER_EXAMPLE(notice:INotice):void
		{
			this.currentApplication = "Libaray";
			var app:Application = AssetsManager.instance.getAsset(this.currentApplication);
			
			if (app == null)
			{
				this.LOAD("assets");
				return;
			}
			
			ApplicationManager.instance.startup(app);
		}
	
	}
}
